package jcrosswords.bo.impl;

import java.util.List;

import jcrosswords.bo.ClueResponseBO;
import jcrosswords.domain.ClueResponse;
import jcrosswords.util.HibernateUtil;

import org.hibernate.Session;

public class ClueResponseBOImpl implements ClueResponseBO {

	@Override
	public List<ClueResponse> getClueResponses(String hint) {
		Session session = HibernateUtil.getSession();
		
		StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append("SELECT id, clue, REPLACE(response, ' ', '') as response, database_id FROM clueresponse");
		
		if(hint != null && hint.trim().length() >= 1) {
			sqlBuilder.append(" WHERE");
			sqlBuilder.append(" REPLACE(response, ' ', '') ILIKE TO_ASCII('"+ hint + "', 'latin2')");
			hint = hint.substring(0, hint.length() - 1);
		}
		return (List<ClueResponse>)session.createSQLQuery(sqlBuilder.toString()).addEntity(ClueResponse.class).list();
	}
	
}